package com.liyuan.yiqixing.service.impl;

import com.liyuan.yiqixing.dao.AuthenticationDao;
import com.liyuan.yiqixing.dao.ItemDao;
import com.liyuan.yiqixing.model.Comment;
import com.liyuan.yiqixing.model.Item;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    ItemDao itemDao;
    @Autowired
    AuthenticationDao authenticationDao;
    @Override
    public Item[] retrieveItems(Integer currentPage, int scale) {
        Item[] items=itemDao.selectItems((currentPage - 1) * scale, scale);
        return items;
    }

    @Override
    public int sum() {
        int sum = itemDao.sum();
        return sum;
    }

    @Override
    public Item selectItem(Integer id) {
        Item item = itemDao.selectItem(id);
        completeUserInfo(item);
        return item;
    }
    private void completeUserInfo(Item item) {
        User user = authenticationDao.retrieveUserById(item.getUser().getId());
        item.getUser().setAvatarUrl(user.getAvatarUrl());
        item.getUser().setRole(user.getRole().getId());
        item.getUser().setLevel(user.getLevel());
        item.getUser().setDescription(user.getDescription());
        item.getUser().setNickname(user.getNickname());
    }

    @Override
    public Item retrievePartOfItem(Integer itemId) {
        Item item = itemDao.selectPartOfItem(itemId);
        completeUserInfo(item);
        return item;
    }
}
